import $ from '../common';
Page({
  data: {
    active: 0,
    info:[],
    videoarr:[],
    page:1,
    tip:'上滑加载很多',
    idx:0,
    PlayAuth:[],
    status:''
  },
  onChange(event) {
    wx.showToast({
      title: `切换到标签 ${event.detail.name}`,
      icon: 'none',
    });
  },
  getdetail(){//获取详情的标题和评分
    const {zonecode,uuid}=this.data
    $.get('/vcourse/detail',{
      zonecode,
      vcourse_uuid:uuid
    },res=>{
      this.setData({
        info:res
      })
    })
  },
  getscore(){//需要积分
    const {zonecode,uuid}=this.data
    $.post('/vcourse/scores/valid',{
      zonecode,
      vcourse_uuid:uuid
    },res=>{
      this.setData({
        need_score:res.need_score
      })
    })
  },
  getvideolist(cb){//视频列表
    const {zonecode,uuid,page,videoarr}=this.data
    $.get('/vcourse/video/list',{
      zonecode,
      page,
      limit:20,
      vcourse_uuid:uuid
    },res=>{
      this.data.page++
      if(res.videos.length<20){
        this.data.tip='已经到底了'
      }else{
        this.data.tip='上滑加载更多'
      }
      let video_id=res.videos[this.data.idx].video_id
      this.setData({
        page:this.data.page,
        tip:this.data.page,
        videoarr:[...videoarr,...res.videos],
        video_id
      })
      if(cb){
        cb()
      }
    })
  },
  getplay(){//暂定
    $.get('/aliyun/vod/play/token',{
      zonecode:this.data.zonecode,
      video_id:this.data.video_id
    },res=>{
      console.log(res);
      this.setData({
        PlayAuth:res.PlayAuth
      })
      // encodeURIComponent
    })
  },
  getcntplay(){//暂定
    $.post('/vcourse/video/cnt/play',{
      zonecode:this.data.zonecode,
      video_id:this.data.video_id
    },res=>{
      console.log(res);
      this.setData({
        status:res.status
      })
    })
  },
  onLoad(e) {
    const {uuid=''}=e
    this.setData({
      uuid,
      zonecode:wx.getStorageSync('addresscode')||''
    })
    this.getdetail()
    this.getscore()
    this.getvideolist(res=>{
      this.getplay()
      this.getcntplay()
    })
  },
  onShow() {
  },
})